<?php

namespace App\Http\Middleware;

use ClassesWithParents\A;
use DefaultConfig;
use Closure;
use Auth;
use Illuminate\Http\Request;

class checkService
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

    private $headers = [
        'content-type' => 'application/json'
    ];

    public function handle(Request $request, Closure $next)
    {
        !empty(Auth::user()) && $permissions = Auth::user()->permissions;
        if(!empty($permissions)){
            foreach ($permissions as $permission){
                if($permission['permission'] == DefaultConfig::SERVICE){
                    return $next($request);
                }
            }
        }

        $response['errorCode'] = config('result.errorNotServiceCode');
        $response['errorMsg']  = config('result.errorNotServiceMsg');
        return response(json_encode($response), 301, $this->headers);
    }
}
